package com.doraemon.controller.user;

import com.doraemon.constant.StatusConstant;
import com.doraemon.entity.Setmeal;
import com.doraemon.result.Result;
import com.doraemon.service.SetmealService;
import com.doraemon.vo.DishItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * c端套餐管理Controller
 **/
@RestController("userSetmealController") // 设置自定义 Bean 名称，防止同名冲突
@RequestMapping("/user/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /***
     * 根据分类id查询套餐
     * -由Spring Cache处理-
     * ---
     * @param categoryId 分类id
     * @return: com.doraemon.result.Result<java.util.List < com.doraemon.entity.Setmeal>>
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "setmealCache", key = "#categoryId") // 构造的缓存key： setmealCache::categoryId的具体值
    @ApiOperation("根据分类id查询套餐")
    public Result<List<Setmeal>> list(Long categoryId) {
        log.info("c端-根据分类id查询套餐：{}", categoryId);

        // 创建套餐实体，并赋值分类id和起售状态（只查询起售状态的套餐）
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);

        // 调用Service层 getListBySetmeal 方法，获取起售状态套餐实体集合
        List<Setmeal> setmeals = setmealService.getListBySetmeal(setmeal);

        return Result.success(setmeals);
    }

    /***
     * 根据套餐id查询包含的菜品列表
     * ---
     * @param id 套餐id
     * @return: com.doraemon.result.Result<java.util.List < com.doraemon.vo.DishItemVO>> 套餐下的菜品列表
     */
    @GetMapping("/dish/{id}")
    @ApiOperation("根据套餐id查询包含的菜品列表")
    public Result<List<DishItemVO>> dishList(@PathVariable("id") Long id) {
        log.info("c端-据套餐id查询包含的菜品列表：{}", id);

        // 调用Service层 getDishItemById 方法，获取套餐包含的菜品列表VO类集合
        List<DishItemVO> dishItemVOs = setmealService.getDishItemVOListById(id);

        return Result.success(dishItemVOs);
    }

}